<script setup lang="ts">
import { ref } from 'vue'
import type { AddPermission } from '@/types/permission'
import { addPermissionApi, getPermissionDetailApi, updatePermissionApi } from '@/api/permission'
import type { FormRules, ElForm } from 'element-plus'
import { ElMessage } from 'element-plus'

// 控制弹框显示隐藏
const dialogVisible = ref(false)
// 开启弹框
const openDialog = (id: string, type: 1 | 2) => {
  title.value = '添加权限'
  // 重置表单数据
  formModel.value = {
    name: '',
    code: '',
    description: '',
    type: 1,
    pid: '',
    enVisible: '0'
  }
  dialogVisible.value = true
  formModel.value.pid = id
  formModel.value.type = type
}
// 关闭弹框
const closeDialog = () => {
  dialogVisible.value = false
  // 重置表单
  formRef.value?.resetFields()
}
const title = ref('')

// 获取表单数据
const formModel = ref<AddPermission>({
  name: '',
  code: '',
  description: '',
  type: 1,
  pid: '',
  enVisible: '0'
})

// 校验规则
const rules = ref<FormRules<AddPermission>>({
  name: [{ required: true, message: '请输入权限名称', trigger: 'blur' }],
  code: [{ required: true, message: '请输入权限标识', trigger: 'blur' }],
  description: [{ required: true, message: '请输入权限描述', trigger: 'blur' }]
})

// 提交表单
const emits = defineEmits<{ (e: 'updataList'): void }>()
const formRef = ref<InstanceType<typeof ElForm> | null>(null)
const submitForm = async () => {
  // 校验表单
  await formRef.value?.validate()
  // 判断 点击的的确定是 添加 还是 编辑
  if (title.value === '添加权限') {
    // 发送请求
    await addPermissionApi(formModel.value)
    // 提示
    ElMessage.success('新增成功')
  } else {
    // 发送请求
    await updatePermissionApi(formModel.value)
    // 提示
    ElMessage.success('编辑成功')
  }
  // 关闭弹框
  closeDialog()
  // 通知父组件
  emits('updataList')
}

// 获取权限详情
// const result = ref<AddPermission>()
const getPermissionDetail = async (id: string) => {
  title.value = '编辑权限'
  const res = await getPermissionDetailApi(id)
  // result.value = res.data
  formModel.value = res.data
  console.log(formModel.value)
  // 打开弹框
  dialogVisible.value = true
}

// 暴露 openDialog方法
defineExpose({ openDialog, getPermissionDetail })
</script>

<template>
  <el-dialog label-width="120px" v-model="dialogVisible" :close-on-click-modal="false" :title="title">
    <el-form :model="formModel" ref="formRef" :rules="rules">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="formModel.name" placeholder="请输入权限名称" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="formModel.code" placeholder="请输入权限标识" />
      </el-form-item>
      <el-form-item label="权限描述" prop="description">
        <el-input v-model="formModel.description" placeholder="请输入权限描述" />
      </el-form-item>
      <el-form-item label="权限开启" prop="enVisible">
        <el-switch v-model="formModel.enVisible" active-value="1" inactive-value="0" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="submitForm"> 确定 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style scoped></style>
